package net.sangpil.applications.viewer
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	import net.sangpil.applications.viewer.event.CameraPositionViewerEvent;
	import net.sangpil.classes.events.MouseDragEvent;
	import net.sangpil.classes.interactions.MouseDrag;

	[Event(name = "camera_move", type = "net.sangpil.applications.viewer.event.CameraPositionViewerEvent")]
	public class CameraPositionViewer extends Sprite
	{
		private var _skinW:Number;
		private var _skinH:Number;
		
		private var _contents:Navi_CameraPosClip;
		private var _plate:Sprite;
		private var _structure:Sprite;
		private var _camera:Sprite;
		
		private var _mouseDrag:MouseDrag;
		
		public function CameraPositionViewer(width:Number, height:Number = 14.25)
		{
			super();
			_skinW = width;
			_skinH = height;
			
			this.addEventListener(Event.ADDED_TO_STAGE, onStage);
		}
		
		private function onStage(e:Event):void
		{
			this.init();
		}
		
		private function init():void
		{
			this._contents = new Navi_CameraPosClip();
			this.addChild(_contents);
			
			this._structure = this._contents.cameraMap_Plane.earth.structure;
			this._camera = this._contents.cameraMap_Plane.earth.camera;
			this._plate = this._contents.cameraMap_Plane.earth.plane;
			
			this.setControlPosition(_skinW, _skinH);
			
			this._structure.alpha = 0.3;
			
			this._structure.buttonMode = this._camera.buttonMode = true;
			_mouseDrag = new MouseDrag(this._camera);
			_mouseDrag.addEventListener(MouseDragEvent.M_MOVE, onMove);
			_mouseDrag.addEventListener(MouseDragEvent.M_UP, onUp);
		}
		
		private var nowMove:Boolean = false;
		private function onMove(e:MouseDragEvent):void
		{
			this.nowMove = true;
			var radians:Number = Math.atan2(0 - this._camera.y, 0 - this._camera.x);
			this._camera.rotation = (radians/Math.PI)*180;
			var point:Point = new Point(e.position.x*ratio, e.position.y*ratio*-1);
			this.dispatchEvent(new CameraPositionViewerEvent(point, CameraPositionViewerEvent.CAMERA_MOVE));
		}
		
		private function onUp(e:MouseDragEvent):void
		{
			this.nowMove = false;
		}
		
		public function upDateCameraPos(px:Number, py:Number, pz:Number, rx:Number, ry:Number, rz:Number):void
		{
			if(!this.nowMove)
			{
				this._camera.x = px/this.ratio;
				this._camera.y = pz/this.ratio*-1;
				var radians:Number = Math.atan2(0 - this._camera.y, 0 - this._camera.x);
				this._camera.rotation = (radians/Math.PI)*180;
			}
		}
		
		public function upDateObjectPos(px:Number, py:Number, pz:Number, rx:Number, ry:Number, rz:Number):void
		{
			this._structure.rotation = ry;
		}
		
		public function setControlPosition(w:Number, h:Number, selectedStr:String = "Camera Position Viewer"):void
		{
			this._contents.cameraMap_Label.bg_second.width = w;
			this._contents.cameraMap_Label.btn_browse.width = w;
			this._contents.cameraMap_Label.txt_url.width = w-10; 
			this._contents.cameraMap_Label.txt_url.text = selectedStr;
			
			this._contents.cameraMap_Plane.width = w;
			this._contents.cameraMap_Plane.height = w;
		}
		
		public function updateCameraDistance(dis:Number):void
		{
			this._contents.txt_distance.text = "Distance : " + dis + " m";
		}
		
		private var mapBitmap:Bitmap;
		private var ratio:Number;
		public function addMap(map:BitmapData):void
		{
			if(this.mapBitmap)
			{
				this.mapBitmap = null;
			}
			
			this.mapBitmap = new Bitmap(map);
			trace(this.mapBitmap.width);
			this.ratio = this.mapBitmap.width/this._plate.width*10;
			this.mapBitmap.width = this._plate.width;
			this.mapBitmap.height = this._plate.height;
			this.mapBitmap.x = this.mapBitmap.width/-2;
			this.mapBitmap.y = this.mapBitmap.height/-2;
			this._plate.addChild(this.mapBitmap);
			
			
			trace("map added : radio : ", ratio);
		}
		
	}
}